package com.xiaohan.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohan.springbootinit.common.BaseResponse;
import com.xiaohan.springbootinit.common.ErrorCode;
import com.xiaohan.springbootinit.common.ResultUtils;
import com.xiaohan.springbootinit.exception.ThrowUtils;
import com.xiaohan.springbootinit.model.dto.Image.ImageQueryRequest;
import com.xiaohan.springbootinit.model.entity.Image;
import com.xiaohan.springbootinit.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 图片接口
 */
@RestController
@RequestMapping("/image")
@Slf4j
public class ImageController {

	@Resource
	private ImageService imageService;

	/**
	 * 分页获取图片（封装类）
	 *
	 * @param imageQueryRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/list/page/vo")
	public BaseResponse<Page<Image>> listImageByPage(@RequestBody ImageQueryRequest imageQueryRequest,HttpServletRequest request) {
		long current = imageQueryRequest.getCurrent();  //当前页码
		long size = imageQueryRequest.getPageSize();
		// 限制爬虫(防止前端传入需要查询每页超过二十条数据的请求)
		ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
		Page<Image> imagePage = imageService.searchImage(imageQueryRequest.getSearchText(), current, size);
		return ResultUtils.success(imagePage);
	}

}
